﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;

namespace QLHPHP
{
    public partial class CapNhapThongTinControl : UserControl
    {
        public CapNhapThongTinControl()
        {
            InitializeComponent();
        }

        private AppCommands _Commands;

        /// <summary>
        /// Gets or sets the commands associated with the control.
        /// </summary>
        public AppCommands Commands
        {
            get { return _Commands; }
            set
            {
                if (value != _Commands)
                {
                    AppCommands oldValue = _Commands;
                    _Commands = value;
                    OnCommandsChanged(oldValue, value);
                }
            }
        }
        /// <summary>
        /// Called when Commands property has changed.
        /// </summary>
        /// <param name="oldValue">Old property value</param>
        /// <param name="newValue">New property value</param>
        protected virtual void OnCommandsChanged(AppCommands oldValue, AppCommands newValue)
        {
            if (newValue != null)
            {
                btnDong.Command = newValue.CapNhapThongTinCommands.Cancel;
                
            }
            else
            {
                btnDong.Command = null;
            }
        }

        private void btnChonSV_Click(object sender, EventArgs e)
        {
            if (txtMSSV.Text != "")
            {

                try
                {
                    int MSSV = int.Parse(txtMSSV.Text);

                    txtSPDK.DataBindings.Add("Text", HocPhi.LaySoPhieuDKMoiNhatTuMSSV(MSSV), "SPMN");
                    int spdk = int.Parse(txtSPDK.Text);

                    txtHoTen.DataBindings.Add("Text", HocPhi.LayThongHPTheoSPDK(spdk), "HoTen");
                    txtNgayDK.DataBindings.Add("Text", HocPhi.LayThongHPTheoSPDK(spdk), "NgayDK");
                    txtHocPhi.DataBindings.Add("Text", HocPhi.LayThongHPTheoSPDK(spdk), "SoTienPhaiDong");
                    txtHocPhiConNo.DataBindings.Add("Text", HocPhi.LayThongHPTheoSPDK(spdk), "SoTienConLai");

                    //txtHoTen.DataBindings.Add("Text",HocPhi.LayThongTinHPTheoMaSV(MSSV),"HoTen");
                    //txtNgayDK.DataBindings.Add("Text",HocPhi.LayThongTinHPTheoMaSV(MSSV),"NgayDK");
                    //txtHocPhi.DataBindings.Add("Text", HocPhi.LayThongTinHPTheoMaSV(MSSV), "SoTienPhaiDong");
                    //txtHocPhiConNo.DataBindings.Add("Text",HocPhi.LayThongTinHPTheoMaSV(MSSV),"SoTienConLai");

                    
                    dgvDSMHDK.DataSource = MonHoc.LayDSMonHocTheoSoPhieuDK(spdk);
                }
                catch
                {
                    ToastNotification.Show(this, "Lỗi!");
                }
            }
            else
            {
                ToastNotification.Show(this, "Vui lòng nhập đủ các trường!");
            }

        }

        private void btnXNDongHocPhi_Click(object sender, EventArgs e)
        {           
            if (txtHocPhiDaDong.Text != "")
            {

                try
                {
                    int day = DateTime.Now.Day;
                    int month = DateTime.Now.Month;
                    int year = DateTime.Now.Year;
                    string ngaylap = day + "/" + month + "/" + year;
                    DateTime nglap = DateTime.Parse(ngaylap);

                    int MSSV = int.Parse(txtMSSV.Text);
                    int spdk = int.Parse(txtSPDK.Text);
                    //int SoTienPhaiDong = int.Parse(txtHocPhi.Text);
                    int sotiendadong = int.Parse(txtHocPhiDaDong.Text);
                    MonHoc.ThemHocPhiVaoPhieuDK(spdk,sotiendadong);
                    HocPhi.TaoPhieuThu(spdk,sotiendadong,nglap);
                    HocPhi.CapNhapThongTinHocPhi();
                    ToastNotification.Show(this, "Đã đóng học phí!");
                }
                catch
                {
                    ToastNotification.Show(this, "Lỗi!");
                }
            }
            else
            {
                ToastNotification.Show(this, "Vui lòng nhập tiền đóng!");
            }


        }

        private void btnCapNhapKQ_Click(object sender, EventArgs e)
        {

            if (txtMSSV.Text != "")
            {
                int mssv = int.Parse(txtMSSV.Text);
                int spdk = int.Parse(txtSPDK.Text);
                try
                {
                    for (int i = 0; i < dgvDSMHDK.Rows.Count - 1; i++)
                    {
                        if (Convert.ToBoolean(dgvDSMHDK.Rows[i].Cells["Pass"].Value) == true)
                        {
                            string MaMH = Convert.ToString(dgvDSMHDK.Rows[i].Cells[2].Value);
                            MonHoc.ThemMonHocTichLuy(mssv, MaMH,spdk);
                            ToastNotification.Show(this,"Đã cập nhật kết quả!");
                        }

                    }
                }
                catch
                {
                    ToastNotification.Show(this, "Lỗi!");
                }
            }
            else
            {
                ToastNotification.Show(this, "Vui lòng nhập mã sinh viên!");
            }
        }
    }
}
